﻿﻿using System;
using StarTrek.Ship;
using StarTrek.World;

namespace StarTrek.Weapon
{
    class Photon:iWeapon
    {
        private int remainingTorpedos = 0;
        private int MaxTorpedos = 0;
        private float torpedoDamage = 0;
        private float range = 0;
        private string name;
        private bool damaged;
        private iShip parent;

        #region iWepon Members

        /// <summary>
        /// This is a Photon torpeado
        /// </summary>
        /// <param name="Name">A Unique name to use with this class of weapon</param>
        /// <param name="MissleBanks">the number of shots this weapon can use</param>
        /// <param name="DamageStrength">the max damage this weapon can perform</param>
        /// <param name="EffectiveRange">the maximum effective range for this weapon</param>
        public Photon(string Name, int MissleBanks, float DamageStrength, float EffectiveRange)
        {
            remainingTorpedos = MissleBanks;
            MaxTorpedos = MissleBanks;
            torpedoDamage = DamageStrength;
            range = EffectiveRange;
            name = Name;
        }
        public Photon(){}
        public ShipStatus ApplyWeponToShip(iShip Target, float Energy)
        {
            ShipStatus rVal = null;
            float dis = Parent.CurrentLocation.CalculateDistance(Target.CurrentLocation);
            float impactDamage = torpedoDamage;
            Random rnd = new Random();
            remainingTorpedos--; //Use a torpedo.
            
            if (0 >= remainingTorpedos) //Zero is the minimum
                remainingTorpedos = 0;

            if (dis >= range)
            {
                //If we are beyond the impact range, then apply a random ammount of damage to the target no greater than the 
                // original impact damage.
                impactDamage = rnd.Next(0, Convert.ToInt32(torpedoDamage));
            }

            if (0 < remainingTorpedos)
            {
                Target.ApplyDamage(impactDamage);
                rVal = Target.CreateShipStatus();
            }

            System.Diagnostics.Debug.WriteLine(String.Format("Applying {0} units of damage to {1}, by firing a torpedo", impactDamage, Target.ShipName));
            return rVal;
        }

        public iShip ApplyDefenseToShip(float Energy)
        {
            throw new InvalidWeaponException(this, this.parent);
        }

        public iShip Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }
        public bool isFunctional
        {
            get
            {
                return !damaged;
            }
            set
            {
                damaged = !value;
            }
        }

        #endregion

        #region iWeapon Members


        public string WeaponReport()
        {
            return String.Format("There are {0} of {1} torpedos remaing", remainingTorpedos, MaxTorpedos);
        }

        #endregion

        #region iWeapon Members


        public void Load(System.Xml.XmlNode node)
        {
            //Msls="5" Range="5" Strength="2000"

            remainingTorpedos = Convert.ToInt32(node.Attributes["Msls"].Value);
            MaxTorpedos = Convert.ToInt32(node.Attributes["Msls"].Value);
            torpedoDamage = Convert.ToSingle(node.Attributes["Strength"].Value);
            range = Convert.ToSingle(node.Attributes["Range"].Value);
            name = node.Attributes["Name"].Value;
        }
        public bool isOffensive { get { return remainingTorpedos > 0; } }
        public bool NeedsEnegery { get { return false; } } 
        public string Name {get { return name;}}
        #endregion
    }
}
